import { login } from "@/api/profile";
import util from "@/libs";

// actions
const actions = {
  async login({ dispatch }, user) {
    user = await login(user);
    util.cookies.set("uuid", user.id);
    util.cookies.set("token", user.id);

    // 设置user信息
    await dispatch(
      "user/set",
      {
        id: user.id,
        username: user.attributes.username,
        nickname: user.attributes.nickname,
        email: user.attributes.email
      },
      { root: true }
    );
    return user;
  },

  async logout({ dispatch }) {
    util.cookies.remove("uuid");
    util.cookies.remove("token");
    // 清空用户信息
    await dispatch("user/set", {}, { root: true });
    return;
  }
};

export default {
  namespaced: true,
  actions
};
